package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.LoginBean;
import com.xuxin.nantai.contract.LoginContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.LoginPresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by xu_xin
 * Created on 2020.06.05 0005
 */
public class LoginModel implements LoginContract.Model {
    @Override
    public void login(LoginPresenter presenter, String user, String password) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .login(user, password)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<LoginBean>() {
            @Override
            public void onNext(LoginBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}